/***************************************************************************
 *   Copyright (C) 2009 by Marian Skvarek   *
 *   skvarek.marian@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A TrackICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "CNoteView.h"
#include "CNote.h"
#include "CDebug.h"
#include "CResources.h"

#include <QGraphicsPixmapItem>
#include <QMenu>
#include <QGraphicsSceneContextMenuEvent>

using namespace easyscore::globals;
using namespace easyscore::utils;

namespace easyscore
{

    namespace core
    {
        CNoteView::CNoteView ( CNote * model )
        {
            mModel = model;
            mPic = 0;
            mAccidentalPic = 0;

            setZValue ( 80 );
        }

        void CNoteView::initialize()
        {

            QAction *removeAction = mMenu.addAction("Remove");
            QObject::connect(removeAction, SIGNAL(triggered()),
                             this, SLOT(remove()));

            mMenu.addSeparator();

            QAction *setSharpAction = mMenu.addAction(CResources::getInstance()->getAccidentalPixmap(eAccidentalSharp),"Sharp");
            QObject::connect(setSharpAction, SIGNAL(triggered()),
                             this, SLOT(setAccidentalToSharp()));

            QAction *setFlatAction = mMenu.addAction(CResources::getInstance()->getAccidentalPixmap(eAccidentalFlat),"Flat");
            QObject::connect(setFlatAction, SIGNAL(triggered()),
                             this, SLOT(setAccidentalToFlat()));

            QAction *setNeutralAction = mMenu.addAction(CResources::getInstance()->getAccidentalPixmap(eAccidentalNatural),"Neutral");
            QObject::connect(setNeutralAction, SIGNAL(triggered()),
                             this, SLOT(setAccidentalToNatural()));

            QAction *setNoneAction = mMenu.addAction(CResources::getInstance()->getAccidentalPixmap(eAccidentalSharp),"None");
            QObject::connect(setNoneAction, SIGNAL(triggered()),
                             this, SLOT(setAccidentalToNone()));

            mMenu.addSeparator();

            QAction *setGhost = mMenu.addAction("Ghost");
            QObject::connect(setGhost, SIGNAL(triggered()),
                             this, SLOT(setGhostNote()));


            mPic = new QGraphicsPixmapItem (CResources::getInstance()->getNotePixmap ( mModel->getValue() ) );
            mPic->setVisible(false);
            mPic->setZValue ( 100 );
            mPic->setParentItem(this);

            mAccidentalPic = new QGraphicsPixmapItem();
            mPic->setVisible(false);
            mAccidentalPic->setZValue(100);
            mAccidentalPic->setParentItem(this);

            setRect ( rect().x(),rect().y(),mPic->boundingRect().width(),mPic->boundingRect().height() );
            showBorder(false);

            mPic->setPos ( 0,0 );
            mAccidentalPic->setPos ( -100, -60 );

            mPic->setVisible(true);

            onAccidentalChanged();

            setAcceptHoverEvents(true);

        }

        void CNoteView::clear()
        {
            if(mPic)
            {
                delete mPic;
                mPic = 0;
            }

            if(mAccidentalPic)
            {
                delete mAccidentalPic;
                mAccidentalPic = 0;
            }
        }

        CNoteView::~CNoteView()
        {
            clear();
        }

        void CNoteView::update()
        {

        }

        void CNoteView::refreshLayout()
        {

        }

        void CNoteView::onAccidentalChanged()
        {
            if(mModel->getAccidental() == eAccidentalNone)
                mAccidentalPic->setVisible(false);
            else
            {
                mAccidentalPic->setPixmap(CResources::getInstance()->getAccidentalPixmap(mModel->getAccidental()));
                mAccidentalPic->setVisible(true);
            }
        }

        void CNoteView::onActiveChanged()
        {
            if(mModel->getIsActive())
            {
                if(mModel->getIsGhost())
                    mPic->setPixmap(CResources::getInstance()->getActiveGhostNotePixmap());
                else
                    mPic->setPixmap(CResources::getInstance()->getActiveNotePixmap(mModel->getValue()));

            }
            else
            {
                if(mModel->getIsGhost())
                    mPic->setPixmap(CResources::getInstance()->getGhostNotePixmap());
                else
                    mPic->setPixmap(CResources::getInstance()->getNotePixmap(mModel->getValue()));
            }
        }

        void CNoteView::hoverEnterEvent ( QGraphicsSceneHoverEvent *)
        {
            mModel->setIsActive(true);
        }

        void CNoteView::hoverLeaveEvent ( QGraphicsSceneHoverEvent * )
        {
            mModel->setIsActive(false);
        }


        void CNoteView::contextMenuEvent(QGraphicsSceneContextMenuEvent* e)
        {
            mMenu.exec( e->screenPos() );
        }

        //Protected slots
        void CNoteView::remove()
        {
            mModel->remove();
        }

        void CNoteView::setAccidentalToSharp()
        {
            mModel->setAccidental(eAccidentalSharp);
        }

        void CNoteView::setAccidentalToFlat()
        {
            mModel->setAccidental(eAccidentalFlat);
        }

        void CNoteView::setAccidentalToNatural()
        {
            mModel->setAccidental(eAccidentalNatural);
        }

        void CNoteView::setAccidentalToNone()
        {
            mModel->setAccidental(eAccidentalNone);
        }

        void CNoteView::setGhostNote()
        {
            mModel->toggleGhost();

            onActiveChanged();
        }


    }
}


